<?php

namespace App\Http\Controllers\Manage;

use App\Model\Admin;
use App\Model\Order;
use App\Model\Product;
use App\Model\Project;
use App\Model\ServiceProvider;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class OrderController extends Controller
{
    protected static $field = [
        ['id'=>1,'searchtype'=>'编号'],
        ['id'=>2,'searchtype'=>'创建时间'],
        ['id'=>2,'searchtype'=>'更新时间'],
        ['id'=>3,'searchtype'=>'名称'],
        ['id'=>4,'searchtype'=>'订单编号'],
        ['id'=>5,'searchtype'=>'项目名称'],
        ['id'=>6,'searchtype'=>'开始时间'],
        ['id'=>7,'searchtype'=>'结束时间'],
        ['id'=>8,'searchtype'=>'服务商'],
        ['id'=>9,'searchtype'=>'价格'],
        ['id'=>10,'searchtype'=>'付款明细'],
    ];

    /**
     * 订单管理
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function orderList()
    {
        $user = session('user');
        $search = Input::get('type');
        $param = Input::get('param');
        $orderBy = Input::get('orderBy');
        $page = Input::get('page');
        $count = Input::get('count');
//        dump(input::all());
        $obj = new Order;
        $id = array();
        $dd_id = DB::table("admin")->where("username",$user["name"])->value("dd_id");
        $root = $this->getDeparUser($dd_id);
        foreach ($root as $value){
            $department[] = array('department_id' => $value["department_id"],'department' => $value["department"]);
            foreach ($value["dd_ids"] as $val){
                if($value["is_leader"]){
                    $person[] =  $val;//负责人
                }
            }
        }
        if(!empty($person)){
            $obj = $obj->whereIn("dd_id",$person);
        }else{
            $obj = $obj->where("dd_id",$dd_id);
        }
        if($search == 1){
            $obj = $obj->where('id','like',"%$param%");
        }elseif ($search == 2){
            $obj = $obj->where('create_time','like',"%$param%");
        }elseif ($search == 3){
            $obj = $obj->where('update_time','like',"%$param%");
        }elseif ($search == 4){
            $obj = $obj->where('name','like',"%$param%");
        }elseif ($search == 5){
            $obj = $obj->where('order_id','like',"%$param%");
        }elseif ($search == 6){
            $project = Project::where('name','like',"%$param%")->get();
            foreach ($project as $i) {
                $id[] = $i['id'];
            }
//            dump($project);
            $obj = $obj->whereIn('project_id',$id);
        }elseif ($search == 7){
            $obj = $obj->where('start_time','like',"%$param%");
        }elseif ($search == 8){
            $obj = $obj->where('stop_time','like',"%$param%");
        }elseif ($search == 9){
            $obj = $obj->where('server','like',"%$param%");
        }elseif ($search == 10){
            $obj = $obj->where('price','like',"%$param%");
        }elseif ($search == 11){
            $obj = $obj->where('detail','like',"%$param%");
        }elseif ($search == 100){
            $obj = $obj->where('status',$param);
        }
        #判断时间排序
        if($orderBy == 1){
            $obj = $obj->orderBy('update_time','ASC');
        }elseif($orderBy == 2){
            $obj = $obj->orderBy('update_time','DESC');
        }else{
            $obj = $obj->orderBy('rank','DESC')->orderBy('update_time','DESC');
        }
        $order = $obj->Paginate(20);
        if(empty($page)){
            $count = $obj->count();
        }
        if($order == false && !empty($search)){
            return redirect()->back()->withSuccess('未搜索到结果！');
        }
        $searchtype = self::$field;
        return view('manage.order.orderlist',[
            'order'=>$order,
            'searchtype'=>$searchtype,
            'search'=>$search,
            'param'=>$param,
            'orderBy'=>$orderBy,
            'count'=>$count,
            'active1' => 4
        ]);
    }

    /**
     * 订单状态修改
     * @return array
     */
    public function orderStatus() {
        $id = Input::get('id');
        $status = Input::get('status');
        if($status == 0) {
            $map = Order::where('id',$id)->update(['status'=>0]);
            $code = 0;
            $msg = '设置成功！';
        }elseif($status == 1){
            $map = Order::where('id',$id)->update(['status'=>1]);
            $code = 1;
            $msg = '设置成功！';
        }elseif($status == 2){
            $map = Order::where('id',$id)->update(['status'=>2]);
            $code = 2;
            $msg = '设置成功！';
        }elseif($status == 3){
            $map = Order::where('id',$id)->update(['status'=>3]);
            $code = 3;
            $msg = '设置成功！';
        }else {
            $map = null;
            $code = 102;
            $msg = '设置失败！';
        }
        if(!$map){
            return [
                'code'=>101,
                'msg'=>'设置失败！'
            ];
        }
        return [
            'code'=>$code,
            'msg'=>$msg
        ];
    }

    public function order_details($id) {
        $order = Order::getOrderInfo($id);
        $att = explode(',',$order['attachment']);
        ###获取全流程详情
        $project_id = $order['project_id'];
        $sale_manage_info = DB::table("sale_manage")->where("project_id",$project_id)->first();
        $sale_step_list = DB::table("sale_step")->where("sale_manage_id",$sale_manage_info['id'])->get();
        foreach ($sale_step_list as &$value){
            if($value['status'] == 1){
                $value['status_str'] = "已完成";
            }else{
                $value['status_str'] = "未完成";
            }
            $admins = explode(",",$value['admin_ids']);
            foreach ($admins as $val){
                $value['admin_ids_str'] .= '【'.Admin::where("dd_id",$val)->value("nickname").'】 ';
            }
        }
        return view('manage.order.order_details',['order'=>$order,'att'=>$att,'active1' => 4,"sale_step_list"=>$sale_step_list]);
    }
}
